package com.swanclouds.redfish.common.redfish;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.swanclouds.api.exceptions.ParameterException;
import com.swanclouds.model.ModelEntity;

/**
 * @author 张立强
 * @version jdk1.8.0
 */
public class IPv4Addresses implements ModelEntity {

    /**
     * IPv4地址
     */
    @JsonProperty("Address")
    private String address;

    /**
     * IPv4地址的子网掩码
     */
    @JsonProperty("SubnetMask")
    private String subnetMask;

    /**
     * IPv4地址获取模式，可设置为“Static”或“DHCP”
     */
    @JsonProperty("AddressOrigin")
    private String addressOrigin;

    /**
     * IPv4网关地址
     */
    @JsonProperty("Gateway")
    private String gateway;

    public static enum AddressOrigin {
        Static,DHCP;
    }

    public static IPv4Addresses builder() {
        return new IPv4Addresses();
    }

    public IPv4Addresses Address(String address) {
        this.address = address;
        return this;
    }

    public IPv4Addresses SubnetMask(String subnetMask) {
        this.subnetMask = subnetMask;
        return this;
    }

    public IPv4Addresses AddressOrigin(String addressOrigin) {
        this.addressOrigin = AddressOrigin.valueOf(addressOrigin).name();
        if (this.addressOrigin == null) {
            throw new ParameterException("AddressOrigin[Static,DHCP], error:" + addressOrigin);
        }
        return this;
    }

    public IPv4Addresses Gateway(String gateway) {
        this.gateway = gateway;
        return this;
    }

    public String getAddress() {
        return address;
    }

    public String getSubnetMask() {
        return subnetMask;
    }

    public String getAddressOrigin() {
        return addressOrigin;
    }

    public String getGateway() {
        return gateway;
    }

    @Override
    public String toString() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
        }
        return "IPv4Addresses {" +
                "address='" + address + '\'' +
                ", subnetMask='" + subnetMask + '\'' +
                ", addressOrigin='" + addressOrigin + '\'' +
                ", gateway='" + gateway + '\'' +
                '}';
    }
}
